package com.efast.dmom.mgmt.openws;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.cafeauto.ws.Message;
import com.efast.cafe.commponent.es.webservice.annotation.CafeWSService;
import com.efast.cafe.commponent.es.webservice.base.WSAbstractAdapter;
import com.efast.cafe.commponent.es.webservice.utils.W3CElementUtil;
import com.efast.cafe.framework.spring.SpringContextUtils;
import com.efast.dmom.mgmt.bean.mes.testline.CarInfoData;
import com.efast.dmom.mgmt.bean.mes.testline.CarInfoDataBean;
import com.efast.dmom.mgmt.bean.mes.testline.CarInfoDataBizContent;
import com.efast.dmom.mgmt.bean.mes.testline.CarInfoInboundDataSet;
import com.efast.dmom.mgmt.bean.mes.testline.TestLineDataLogBean;
import com.efast.dmom.mgmt.service.mes.testline.impl.TestLineService;

@CafeWSService(bizCode = "CarInfo_Test")
public class CarInfoDataService extends WSAbstractAdapter {

	@Override
	public Message handleMessage(Message message) {
		
		logger.info("Received CarInfo data from: " + message.getBIZ_CODE());

		CarInfoDataBizContent content = W3CElementUtil.elementUnmarshalToObj(message.getBIZ_CONTENT(),
				CarInfoDataBizContent.class);
		CarInfoInboundDataSet inboundData = content.getCarInfoInboundDataSet();
		List<CarInfoData> dataList = inboundData.getCarInfoData();

		if(dataList != null && dataList.size() >0){
			
			List<CarInfoDataBean> list = new ArrayList<CarInfoDataBean>();
			
			for (CarInfoData data : dataList) {

				CarInfoDataBean dataBean = new CarInfoDataBean();

				dataBean.setVin(data.getVin());
				dataBean.setCarType(data.getCarType());
				dataBean.setDrivingForm(data.getDrivingForm());
				dataBean.setFarLightAdjustment(data.getFarLightAdjustment());
				dataBean.setFuelType(data.getFuelType());
				dataBean.setLampSystem(data.getLampSystem());
				dataBean.setMotorNumber(data.getMotorNumber());
				dataBean.setMotorType(data.getMotorType());
				dataBean.setReportNumber(data.getReportNumber());
				dataBean.setResult(data.getResult());
				dataBean.setSuspensionForm(data.getSuspensionForm());
				dataBean.setTestTime(data.getTestTime());
				dataBean.setTestNumber(data.getTestNumber());

				dataBean.setCreateTime(new Date());
				dataBean.setCreateUser(message.getBIZ_CODE());

				list.add(dataBean);
			}

			TestLineService service = SpringContextUtils.getBean(TestLineService.class);
			service.insertCarInfoData(list);
			
			TestLineDataLogBean logBean = new TestLineDataLogBean();
			logBean.setVin(dataList.get(0).getVin());
			logBean.setStatus("保存");
			logBean.setReceivedTime(new Date());
			logBean.setSource(message.getMESSAGE_FROM());
			logBean.setDestination(message.getMESSAGE_TO());
			
			service.insertTestLineLog(logBean);
		}
		

		message.setBIZ_CONTENT(null);
		message.setMESSAGE_FROM("MES");
		message.setMESSAGE_TO("CarInfo_Test");
		message.setRET_CODE("200");
		message.setRET_CODE_DESC("Successful");
		return message;

	}

}
